package main.java.com.epam;

import java.util.ArrayList;
import java.util.List;

public class PrimaryMyltNumbers {

	public static List<Long> getPrimaryMultNums(Long number){

		if(number < 1) throw new RuntimeException();
		
		//get list of primary numbers 0..sqrt(number)
		List<Integer> primaryNumbers = getPrimaryNumbers(number);

		List<Long> primaryMultNumbers = new ArrayList<Long>();

		//check only for prime numbers 
		for(Integer primaryNumber:primaryNumbers){
			while(number % primaryNumber == 0){
				number /= primaryNumber;
				primaryMultNumbers.add((long)primaryNumber);
			}
		}
		if (primaryMultNumbers.isEmpty()) primaryMultNumbers.add(number);
			
		return primaryMultNumbers;
	}


	private static List<Integer> getPrimaryNumbers(long number){

		List<Integer> primaryNumbers = new ArrayList<Integer>();
		primaryNumbers.add(2);
		int edge = (int) Math.sqrt(number);

		for(int i = 3; i < edge; i += 2){
			for(int j = 0; j <= primaryNumbers.size(); j++){
				if (i % primaryNumbers.get(j) == 0) break;
				else if(j == primaryNumbers.size() - 1)
					primaryNumbers.add(i);
			}
		}
		return primaryNumbers;
	}


}
